#include "pcbasicintform.h"
#include "ui_pcbasicintform.h"
#include"liwishell.h"
#include"data.h"
#include<QTimer>
#include<QMessageBox>

PCBasicIntForm::PCBasicIntForm(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::PCBasicIntForm)
{
    ui->setupUi(this);

    connect(ui->pushButton_do1,&QPushButton::clicked,this,&PCBasicIntForm::toDo1);
    connect(ui->pushButton_do2,&QPushButton::clicked,this,&PCBasicIntForm::toDo2);
    connect(ui->pushButton_stop,&QPushButton::clicked,this,&PCBasicIntForm::on_PushButton_stop_clicked);
    connect(ui->pushButton_clean,&QPushButton::clicked,this,&PCBasicIntForm::on_PushButton_cleanpos_clicked);
    connect(ui->pushButton_read,&QPushButton::clicked,this,&PCBasicIntForm::readValue);

    QTimer *timer1=new QTimer();
    connect(timer1,&QTimer::timeout,this,&PCBasicIntForm::updateWin);
    timer1->start(100);


}

PCBasicIntForm::~PCBasicIntForm()
{
    delete ui;
}

//动作1
void PCBasicIntForm::toDo1(){
    float array[3];
    array[0]=(float)ui->spinBox_setx1->value();
    array[1]=(float)ui->spinBox_sety1->value();
    array[2]=(float)ui->spinBox_setz1->value();
    PMC_SetUserArray(Data::handle,"array_para",0,3,array);
    if(runflage){
        QMessageBox::information(this,tr("warn"),"请等待动作2完成");
        return;
    }
    runflage=true;
    ui->label_flag->setText("运行中");
    ui->label_flag->setStyleSheet("background-color: rgb(0,0,255)");
    ui->pushButton_do1->setEnabled(true);
    QThread::sleep(0.5);

    float cmd=1;
    ui->label_cmd->setText(QString::number(cmd));
    PMC_SetUserVar(Data::handle,"cmd_data",cmd);

    // PMC_SetUserVar(Data::handle,"cmd",cmd);
}

//动作2
void PCBasicIntForm::toDo2(){
    float array[3];
    array[0]=(float)ui->spinBox_setx2->value();
    array[1]=(float)ui->spinBox_sety2->value();
    array[2]=(float)ui->spinBox_setz2->value();
    PMC_SetUserArray(Data::handle,"array_para",5,3,array);
    if(runflage){
        QMessageBox::information(this,tr("warn"),"请等待动作1完成");
        return;
    }
    runflage=true;
    ui->label_flag->setText("运行中");
    ui->label_flag->setStyleSheet("background-color: rgb(0,0,255)");
    ui->pushButton_do2->setEnabled(true);

    QThread::sleep(0.5);
    float cmd=2;    
    ui->label_cmd->setText(QString::number(cmd));
    PMC_SetUserVar(Data::handle,"cmd_data",cmd);
    // PMC_SetUserVar(Data::handle,"cmd",cmd);
}



//停止
void PCBasicIntForm::on_PushButton_stop_clicked(){
    if(Data::handle!=nullptr){
        float cmd=3;
        ui->label_cmd->setText(QString::number(cmd));
        PMC_SetUserVar(Data::handle,"cmd_data",cmd);
        int value[3]={0,0,0};
        for (int i = 0; i < 3; ++i) {
            PMC_GetIfIdle(Data::handle,i,&value[i]);
        }
        if(value[0]==-1&&value[1]==-1&&value[2]==-1){
            runflage=false;
            ui->label_flag->setText("停止");
            ui->label_flag->setStyleSheet("background-color: rgb(255,0,0)");
            ui->pushButton_do1->setEnabled(true);
            ui->pushButton_do2->setEnabled(true);
        }
    }

}

//坐标清零
void PCBasicIntForm::on_PushButton_cleanpos_clicked(){
    if(Data::handle!=nullptr){
        float cmd=4;
        ui->label_cmd->setText(QString::number(cmd));
        PMC_SetUserVar(Data::handle,"cmd_data",cmd);
    }
}

//读取值
void PCBasicIntForm::readValue(){
    float m_ArrayShow[10];
    float cmd=0;
    PMC_GetUserVar(Data::handle,"cmd_data",&cmd);   //读取全局变量
    PMC_GetUserArray(Data::handle,"array_para",0,9,m_ArrayShow);    //读取全局数组,注意BASIC定义的数组最好比PC端实际数据大1，BASIC数组最后一个元素访问时会越界

    ui->label_cmd->setText(QString::number(0));
    ui->spinBox_readx1->setValue(m_ArrayShow[0]);
    ui->spinBox_ready1->setValue(m_ArrayShow[1]);
    ui->spinBox_readz1->setValue(m_ArrayShow[2]);
    ui->spinBox_readx2->setValue(m_ArrayShow[5]);
    ui->spinBox_ready2->setValue(m_ArrayShow[6]);
    ui->spinBox_readz2->setValue(m_ArrayShow[7]);

    // PMC_SetUserVar(Data::handle,"cmd",cmd);
}

//更新窗口
void PCBasicIntForm::updateWin(){
    float pos=0;
    for (int i = 0; i < 3; ++i) {
        PMC_GetDpos(Data::handle,i,&pos);
        if(i==0) ui->label_xpos->setText(QString::number(pos));
        else if(i==1) ui->label_ypos->setText(QString::number(pos));
        else if(i==2) ui->label_zpos->setText(QString::number(pos));
    }
    if(runflage){
        int value[3]={0,0,0};

        for (int i = 0; i < 3; ++i) {
            PMC_GetIfIdle(Data::handle,i,&value[i]);
        }
        if(value[0]==-1&&value[1]==-1&&value[2]==-1){
            runflage=false;
            ui->label_flag->setText("停止");
            ui->label_flag->setStyleSheet("background-color: rgb(255,0,0)");
            ui->pushButton_do1->setEnabled(true);
            ui->pushButton_do2->setEnabled(true);
        }
    }


}
